package com.canbot.u05.adapter.dance

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.CheckBox
import android.widget.ImageView
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.canbot.u05.R
import com.canbot.u05.activity.logd
import com.canbot.u05.bean.dance.DancePicItemBean
import com.canbot.u05.glide.GlideApp
import com.canbot.u05.socket.IpMsgConst
import kotlinx.android.synthetic.main.dance_choose_pic_item.view.*

class DancePicAdapter(internal val mContext: Context, private val mbrowList: MutableList<DancePicItemBean>?) : RecyclerView.Adapter<DancePicAdapter.MyViewHolder>() {

        private var index: Int = 0
        private var currentPos = -1//current
        inner class MyViewHolder(view: View,parent: ViewGroup) : RecyclerView.ViewHolder(view) {
                var myViewGroup = parent
                var iconImageView: ImageView = view.image
                var btn_sure = view.btn_sure

        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
                val view = LayoutInflater.from(mContext).inflate(R.layout.dance_choose_pic_item, parent, false)
                return MyViewHolder(view,parent)
        }

        override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
                val (isChecked,imageUrl, function) = mbrowList!![position]
                logd("imageUrl:" + imageUrl)
                GlideApp.with(mContext).clear(holder.iconImageView)
                GlideApp.with(mContext)
                        .asDrawable()
                        .load(IpMsgConst.HTTP_URL_CHEST + imageUrl)
                        .thumbnail(0.1f)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存策略
                        .skipMemoryCache(false)
                        .placeholder(R.drawable.sales_banner01)//占位图
                        .error(R.drawable.ic_picture_loadfailed)
                        .into(holder.iconImageView)

                holder.btn_sure.isSelected = isChecked
                holder.iconImageView.setOnClickListener{
                        //设置选中
                        mbrowList.forEach {
                                it.isChecked = false
                                holder.btn_sure.isSelected = false
                        }
                        val dancePicItemBean = mbrowList.get(position)
                        if (holder.btn_sure.isSelected){
                                holder.btn_sure.isSelected = false
                                dancePicItemBean.isChecked = false

                        }else{
                                holder.btn_sure.isSelected = true
                                dancePicItemBean.isChecked = true
                        }
                        mbrowList[position] = dancePicItemBean
                        function(position,mbrowList.get(position).imageUrl)
                }
        }

        override fun getItemId(i: Int): Long {
                return i.toLong()
        }

        override fun getItemCount(): Int {
                return mbrowList?.size ?: 0
        }

        fun setIndex(i: Int) {
                index = i
        }
}
